/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view.command;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.ExtensionFileFilter;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;

/**
 * This class is an implementation of the interface Command.
 * It's unabled to undo or redo.
 * Its "execute" opens a stored model calling
 * a file chooser to get the file.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since Febraury 2004
 * @see gui.command.Command
 */
public class OpenCommand implements Command {
    
    private LearnInterface parent;
    private JFileChooser fileChooser = new JFileChooser();
    private ExtensionFileFilter filter = new ExtensionFileFilter("isn", "INSANE Files");
    private boolean validFile;
	
//*****************************************************************************
	
    /**
     * Creates new OpenCommand
     * and sets the FileFilter do FileChooser.
     *
     * @param parent The JFrame parent.
     */
    public OpenCommand(JFrame parent) {
        this.parent = (LearnInterface)parent;
        fileChooser.setFileFilter(filter);
    }
    
//*****************************************************************************
	
    /**
     * Calls the file chooser and opens a stored model.
     *
     * @see gui.command.Command#execute()
     */
    public void execute() {
        if (parent.closeModel()) {
			validFile = false;
			while (!validFile) {
				int retVal = fileChooser.showOpenDialog(parent);
				if (retVal == JFileChooser.APPROVE_OPTION) {
					//Checks if the file exists and is readable
					File file = fileChooser.getSelectedFile();
					if (file.exists() && file.isFile() && file.canRead()){
						validFile = true;
					}
					else {
						JOptionPane.showMessageDialog(parent, "File \"" + fileChooser.getSelectedFile().getPath() + "\" doesn't exist.", "Error Message", JOptionPane.ERROR_MESSAGE);
					}
				}
				else return;
			}
			
			try{
				ObjectInputStream stream = new ObjectInputStream(new FileInputStream(fileChooser.getSelectedFile()));
				parent.setModel((FemModel)stream.readObject());
				parent.getView().getViewState().setPath(fileChooser.getSelectedFile().getPath());
				parent.getView().getViewState().setName(fileChooser.getSelectedFile().getName());
				parent.setInitialInterface();
			}catch (Exception exc) {
				Toolkit.getDefaultToolkit().beep();
				JOptionPane.showMessageDialog(parent, "Couldn't open file!", "Error Message", JOptionPane.ERROR_MESSAGE);
			}
			parent.getView().nothingToUndo();
			parent.getView().nothingToRedo();
			parent.getView().getDrawingArea().setZoom(parent.getView().getViewState().getCurrentZoom());
			parent.getConsolePanel().clear();
			parent.getConsolePanel().requestFocus();
			parent.getConsoleHandler().setToCommandMode();
			parent.getConsoleHandler().setKeybCommand(null);
			parent.setTitle("INSANE " + parent.getProperties().getVersion() + " - " + parent.getView().getViewState().getName());
			parent.getSplitPane().setDividerLocation(230);
			parent.getView().getController().compose();
			
			parent.getView().getDrawingArea().repaint();
		}
	}
    
//*****************************************************************************
	
    /**
     * Impossible to undo - not implemented.
     */
    public void undo() {}
    
//*****************************************************************************
	
    /**
     * Impossible to redo - not implemented.
     */
    public void redo() {}
    
//*****************************************************************************
}

